/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.EJB.sessionBeans;

import it.polimi.meteocal.EJB.entities.Calendar;
import it.polimi.meteocal.EJB.entities.Event;
import it.polimi.meteocal.EJB.entities.Group;
import it.polimi.meteocal.EJB.entities.Invite;
import it.polimi.meteocal.EJB.entities.ModificationNotification;
import it.polimi.meteocal.EJB.entities.User;
import it.polimi.meteocal.EJB.entities.WeatherNotification;
import it.polimi.meteocal.EJB.gui.LoginBean;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Fabio
 */
@Stateless
public class UserManager {

    @PersistenceContext
    private EntityManager em;

    @Resource
    private EJBContext context;

    public void create(User user) {
        user.setCalendar(new Calendar(user.getUsername(), user.getPrivate1()));
        List<Event> createdEvents = new ArrayList<Event>();
        List<ModificationNotification> mNList = new ArrayList<ModificationNotification>();
        List<WeatherNotification> wNList = new ArrayList<WeatherNotification>();
        List<Invite> invitedEvents = new ArrayList<Invite>();
        user.setEventList(createdEvents);
        user.setInviteList(invitedEvents);
        user.setWeatherNotificationList(wNList);
        user.setModificationNotificationList(mNList);
        user.setGroupname(Group.USER.toString());
        em.persist(user);

    }

    public void update(User user) {
        em.merge(user);
    }
    
    public void delete(User user){
        User u= find(user.getUsername());
        em.remove(u);
    }

    public User find(String username) {
        return em.find(User.class, username);
    }

    public String getLoggedUserUN() {
        return context.getCallerPrincipal().getName();
    }

    public User getLoggedUser() {
        return find(getLoggedUserUN());
    }

    public List<Long> userEventList(String a){
        Query q = em.createNativeQuery("SELECT id FROM Event WHERE organizer = '" + a +"' ");
        List<Long> eventresults = q.getResultList();
        return eventresults;
    }
    
    public List<Long> userInviteList(String a){
        Query q = em.createNativeQuery("SELECT id FROM Invite WHERE guest = '" + a +"' ");
        List<Long> eventresults = q.getResultList();
        return eventresults;
    }
    
    public List<Long> userEventListPublic(String a){
        Query q = em.createNativeQuery("SELECT id FROM Event WHERE organizer = '" + a +"'  AND private = false");
        List<Long> eventresults = q.getResultList();
        return eventresults;
    }
    
    public List<Long> userInviteListPublic(String a){
        Query q = em.createNativeQuery("SELECT id FROM Invite WHERE guest = '" + a +"' AND private = false");
        List<Long> eventresults = q.getResultList();
        return eventresults;
    }
    
    
}
